package tj.eskhata.pos.web.output;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.PageParameters;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.ReuseIfModelsEqualStrategy;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.AbstractReadOnlyModel;

import tj.eskhata.pos.domain.Client;

public class ClientListPanel extends Panel {
	private String strName="";
	private Client client;
	public String getStrName() {
		return this.strName;
	}
	public void setStrName(String strName) {
		this.strName=strName;
	}
	
	public  ClientListPanel(String id,String strn,String strp) {
		super(id);
		SortableContactDataProvider dp;
	
		dp = new SortableContactDataProvider(strn,strp);
	
		final DataView<Client> dataView = new DataView<Client>("oir", dp)
		{
			@Override
			protected void populateItem(final Item<Client> item)
			{	
				client = item.getModelObject();
				//item.add(new ActionPanel("actions", item.getModel()));
				item.add(new Link("select")
				{
					@Override
					public void onClick()
					{
						client = (Client)item.getDefaultModelObject();
						setResponsePage(new Output2(client));						
					}
				});
				item.add(new Link("edit")
				{
					@Override
					public void onClick()
					{
						client = (Client)item.getDefaultModelObject();
						setResponsePage(new NewClient(client));						
					}
				});
				/*item.add(new Link("toggleHighlite")
				{
					@Override
					public void onClick()
					{
						HighlitableDataItem<Client> hitem = (HighlitableDataItem<Client>)item;
						hitem.toggleHighlite();
					}
				});
				*/			
				item.add(new Label("name", client.getName()));
				String sCert="";
				
				if(client.getCertificate()!=null){
					if(client.getCertificate().getSeria()!=null)
						sCert=sCert+client.getCertificate().getSeria().toString();
					
					if(client.getCertificate().getDocNum()!=null)
						sCert=sCert+client.getCertificate().getDocNum().toString();
				}
				
				item.add(new Label("certificate", sCert));						
	
				item.add(new AttributeModifier("class", true, new AbstractReadOnlyModel<String>()
				{
					@Override
					public String getObject()
					{
						return (item.getIndex() % 2 == 1) ? "even" : "odd";
					}
				}));
			}
			
		};
	
		dataView.setItemsPerPage(15);
		dataView.setItemReuseStrategy(ReuseIfModelsEqualStrategy.getInstance());
		/*
		add(new OrderByBorder("orderByFirstName", "firstName", dp)
		{
			@Override
			protected void onSortChanged()
			{
				dataView.setCurrentPage(0);
			}
		});
	
		add(new OrderByBorder("orderByLastName", "lastName", dp)
		{
			@Override
			protected void onSortChanged()
			{
				dataView.setCurrentPage(0);
			}
		});
	*/
	
		add(dataView);
		add(new PagingNavigator("navigator", dataView));
	}
}
